<?php
include "./include/om_config.inc";
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $smarty = new SmartyWWW();

	$commoNameValuesArr[0]  = "ALUMINIUM";
	$commoNameValuesArr[1]  = "CARDAMOM";
	$commoNameValuesArr[2]  = "COPPER";
	$commoNameValuesArr[3]  = "CPO";
	$commoNameValuesArr[4]  = "CRUDEOIL";
	$commoNameValuesArr[5]  = "GOLD";
	$commoNameValuesArr[6]  = "LEAD";
	$commoNameValuesArr[7]  = "MENTHAOIL";
	$commoNameValuesArr[8]  = "NATURALGAS";
	$commoNameValuesArr[9]  = "NICKEL";
	$commoNameValuesArr[10] = "POTATO";
	$commoNameValuesArr[11] = "SILVER";
	$commoNameValuesArr[12] = "ZINC";
 
  $filePath       = '';
  $fileName       = '';
  $expiryDate     = "";
  $expiryDateBc   = '';
  $bhavcopyDate   = '';
  $openPrice      = '';
  $highPrice      = '';
  $lowPrice       = '';
  $closePrice     = '';
  
  
  if(isset($_POST['submitBtn']))
  {
    $uploaddir = dirname($_POST['filePath']);

    $uploadfile = $uploaddir . basename($_FILES['fileName']['name']);
    
    $target_path = './bhavcopiesmcx';
    $target_path = $target_path ."/". basename( $_FILES['fileName']['name']);
    $_FILES['fileName']['tmp_name']; // temp file
    
    $oldfile =  basename($_FILES['fileName']['name']);

    // getting the extention

    $pos = strpos($oldfile,".",0);
    $ext = trim(substr($oldfile,$pos+1,strlen($oldfile))," ");
     
    if(move_uploaded_file($_FILES['fileName']['tmp_name'], $target_path)) 
    {
    	$row = 0;
      $handle = fopen($target_path, "r");
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
      {
	      if($row > 0)
	      {
	      	$exchange		    =	'MCX';
	        $contractCode   = $data[1];
	        $openPrice      = $data[3];
	        $highPrice      = $data[4];
	        $lowPrice       = $data[5];
	        $closePrice     = $data[6];
	        $expiryDateStr  = strtoupper(substr($data[2],3,3));
	        $monthStr       = strtoupper(substr($data[0],3,3));
	       
	        switch ($expiryDateStr)
		    	{
		    	  case "JAN":
		    	    $expiryMonthInt = '01';
		    	    break;
		    	  case "FEB":
		    	    $expiryMonthInt = '02';
		    	    break;
		    	  case "MAR":
		    	    $expiryMonthInt = '03';
		    	    break;
		    	  case "APR":
		    	    $expiryMonthInt = '04';
		    	    break;
		    	  case "MAY":
		    	    $expiryMonthInt = '05';
		    	    break;
		    	  case "JUN":
		    	    $expiryMonthInt = '06';
		    	    break;
		    	  case "JUL":
		    	    $expiryMonthInt = '07';
		    	    break;
		    	  case "AUG":
		    	    $expiryMonthInt = '08';
		    	    break;
		    	  case "SEP":
		    	    $expiryMonthInt = '09';
		    	    break;
		    	  case "OCT":
		    	    $expiryMonthInt = '10';
		    	    break;
		    	  case "NOV":
		    	    $expiryMonthInt = '11';
		    	    break;
		    	  case "DEC":
		    	    $expiryMonthInt = '12';
		    	    break;
		    	}

	        switch ($monthStr)
		    	{
		    	  case "JAN":
		    	    $monthInt = '01';
		    	    break;
		    	  case "FEB":
		    	    $monthInt = '02';
		    	    break;
		    	  case "MAR":
		    	    $monthInt = '03';
		    	    break;
		    	  case "APR":
		    	    $monthInt = '04';
		    	    break;
		    	  case "MAY":
		    	    $monthInt = '05';
		    	    break;
		    	  case "JUN":
		    	    $monthInt = '06';
		    	    break;
		    	  case "JUL":
		    	    $monthInt = '07';
		    	    break;
		    	  case "AUG":
		    	    $monthInt = '08';
		    	    break;
		    	  case "SEP":
		    	    $monthInt = '09';
		    	    break;
		    	  case "OCT":
		    	    $monthInt = '10';
		    	    break;
		    	  case "NOV":
		    	    $monthInt = '11';
		    	    break;
		    	  case "DEC":
		    	    $monthInt = '12';
		    	    break;
		    	}
		    	$expiryDate       = substr($data[2],7,4)."-".$expiryMonthInt."-".substr($data[2],0,2);
		    	$bhavcopyDate     = substr($data[0],7,4)."-".$monthInt."-".substr($data[0],0,2);
		    	$expiryDateStrIns = substr($data[2],7,4)."-".$expiryMonthInt."-".substr($data[2],0,2);

          //
          $key = in_array($contractCode, $commoNameValuesArr);
          //
	        if($key)
	        {
		         $insertBhavcopy = "INSERT INTO bhavcopy (exchange,bhavcopyDate,contractCode,expiryDate,expiryDateBc,
		        	                                         openPrice,highPrice,lowPrice,closePrice,openInterest)
		        	                   VALUES ('".$exchange."','".$bhavcopyDate."','".$contractCode."','".$expiryDate."','".$expiryDateStrIns."',
		        	                           '".$openPrice."','".$highPrice."','".$lowPrice."','".$closePrice."',".$data[11].")";
		        mysql_query($insertBhavcopy);
			    }
			  }
		    $row++;
      }
    } 
    else
      echo "There was an error uploading the file, please try again!";
  }
  $selectQuery = "SELECT * FROM general";
  $selectResult = mysql_query($selectQuery);
  if($selectRow = mysql_fetch_array($selectResult))
  {
    $filePath = $selectRow['filePath'];
    $fileName = $selectRow['fileName'];
  }
  
  $headMenu  = "<B>Tips : &nbsp;&nbsp;</B><A href='./txtFileF_O.php?exc=F_O'>SubmitF_O </A>&nbsp;&nbsp;&nbsp;";

  $smarty->assign("headMenu", $headMenu);
}
?>
<BODY><br>

<H1><CENTER><FONT color="red">TIPS</FONT></CENTER></H1><HR>

<CENTER>
  <FORM enctype="multipart/form-data" action="" name="form1" method="POST"> 
	<a href="index.php">Home</a>&nbsp;&nbsp;&nbsp;	<a href="bhavCopyListMcx.php">List Mcx</a>
	<br /><br />
	<input type="hidden" name="exc" value="F_O">
<INPUT size="60" type="hidden" name="filePath"><BR>
File Name: <INPUT name="fileName" type="file" onChange="document.form1.filePath.value=document.form1.fileName.value;">
<INPUT type="submit" name="submitBtn" value="Submit File"> 
</FORM>
</CENTER>
